package com.sun.local.analysis;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 分析生成数据
 * 备注：不需要联网，需要先获取到历史数据文件。
 */
public class AnalysisGenerateData {

    public static final String READ_PATH = System.getProperty("user.dir") + File.separator + "data" +
            File.separator + "filter" + File.separator + "ssq_filter_top.text";
    public static final String WRITE_PATH = System.getProperty("user.dir") + File.separator + "data" +
            File.separator + "filter" + File.separator + "ssq_filter_top_analysis.text";

    public static void main(String[] args) {
        // 读取文件数据
        List<String> ssqList = FileUtil.readUtf8Lines(READ_PATH);
        List<String> outputList = new ArrayList<>();
        for (String ssq : ssqList) {
            // 组合列表
            List<Integer> combinationList = new ArrayList<>();
            for (String s : ssq.split("\\+")[0].split(",")) {
                combinationList.add(Integer.parseInt(s));
            }
            // AC值
            int acValue = calcACValue(combinationList);
            // 连号数
            int serialNumber = calcSerialNumber(combinationList);
            // 和值
            int sumValue = calcSum(combinationList);
            // 奇偶比
            String oddEvenRatio = calcOddEven(combinationList);
            // 分布
            String distribution = calcDistribution(combinationList);
            // 写出内容
            String outputContent = getOutputContent(ssq, acValue, serialNumber, sumValue, oddEvenRatio, distribution);
            outputList.add(outputContent);
        }
        // 清空文件内容
        FileUtil.writeUtf8String("", WRITE_PATH);
        // 写出数据
        FileUtil.appendUtf8Lines(outputList, WRITE_PATH);
        System.out.println("数据分析完成！");
    }

    /**
     * 计算AC值
     *
     * @param combinationList 单注组合
     * @return AC值
     */
    private static int calcACValue(List<Integer> combinationList) {
        int num1 = combinationList.get(0);
        int num2 = combinationList.get(1);
        int num3 = combinationList.get(2);
        int num4 = combinationList.get(3);
        int num5 = combinationList.get(4);
        int num6 = combinationList.get(5);

        Set<Integer> set = new HashSet<>();
        set.add(num6 - num1);
        set.add(num6 - num2);
        set.add(num6 - num3);
        set.add(num6 - num4);
        set.add(num6 - num5);

        set.add(num5 - num1);
        set.add(num5 - num2);
        set.add(num5 - num3);
        set.add(num5 - num4);

        set.add(num4 - num1);
        set.add(num4 - num2);
        set.add(num4 - num3);

        set.add(num3 - num1);
        set.add(num3 - num2);

        set.add(num2 - num1);

        // AC值公式：AC = R - (N - 1) -> N = 6
        return set.size() - (6 - 1);
    }

    /**
     * 计算连号数
     */
    private static int calcSerialNumber(List<Integer> combinationList) {
        if(CollectionUtils.isEmpty(combinationList)) {
            return 0;
        }
        // 所有连号组合
        List<String> serialNumberList = new ArrayList<>();
        for (int i = 0; i < combinationList.size() - 1; i++) {
            String serialNumber = "";
            int front = combinationList.get(i);
            int after = combinationList.get(i + 1);
            if((front + 1) == after) {
                serialNumber = front + "-" + after;

                for (String s : serialNumberList) {
                    String[] split = s.split("-");
                    String smallNo = split[0];
                    String bigNo = split[split.length - 1];
                    if(StringUtils.equals(String.valueOf(front), bigNo)) {
                        serialNumber = smallNo + "-" + after;
                    }
                }
                serialNumberList.add(serialNumber);
            }
        }
        // 最大连号数量
        int maxCount = 0;
        for (String s : serialNumberList) {
            String[] split = s.split("-");
            int diff = Integer.parseInt(split[1]) - Integer.parseInt(split[0]) + 1;
            if(diff > maxCount) {
                maxCount = diff;
            }
        }
        return maxCount;
    }

    /**
     * 计算和值
     */
    private static int calcSum(List<Integer> combinationList) {
        return combinationList.stream().mapToInt(Integer::intValue).sum();
    }

    /**
     * 计算奇偶比
     */
    private static String calcOddEven(List<Integer> combinationList) {
        // 奇数的数量
        int oddNumbers = 0;
        // 偶数的数量
        int evenNumbers = 0;
        for (Integer integer : combinationList) {
            // 奇数
            if (integer % 2 != 0) {
                oddNumbers++;
            }
            // 偶数
            if (integer % 2 == 0) {
                evenNumbers++;
            }
        }
        return oddNumbers + ":" + evenNumbers;
    }

    /**
     * 计算分布
     *
     */
    private static String calcDistribution(List<Integer> combinationList) {
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        for (Integer value : combinationList) {
            if (value <= 11) {
                count1++;
            }
            if (value > 11 && value <= 22) {
                count2++;
            }
            if (value > 22 && value <= 33) {
                count3++;
            }
        }
        return count1 + ":" + count2 + ":" + count3;
    }

    /**
     * 获取写出内容
     */
    private static String getOutputContent(String ssq, int acValue, int serialNumber, int sumValue, String oddEvenRatio, String distribution) {
        StringBuilder content = new StringBuilder(ssq);
        content.append("|AC=").append((acValue >= 10) ? acValue : acValue + " ");
        content.append("|SerialNumber=").append(serialNumber);
        content.append("|SUM=").append((sumValue >= 100) ? sumValue : sumValue + " ");
        content.append("|OddEvenRatio=").append(oddEvenRatio);
        content.append("|Distribution=").append(distribution);
        return content.toString();
    }

}
